package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;
import java.util.PriorityQueue;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1202 {
    public long startExecuteTime = System.currentTimeMillis();


    /**
     * Created by fanzhenyu02 on 2020/6/27.
     * Union Find 算法实现
     */
    public class UF {
        // 连通分量个数
        private int count;
        // 存储一棵树
        private int[] parent;
        // 记录树的“重量”
        private int[] size;

        public UF(int n) {
            this.count = n;
            parent = new int[n];
            size = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
                size[i] = 1;
            }
        }

        /* 将 p 和 q 连接 */
        public void union(int p, int q) {
            int rootP = find(p);
            int rootQ = find(q);
            if (rootP == rootQ)
                return;

            // 小树接到大树下面，较平衡
            if (size[rootP] > size[rootQ]) {
                parent[rootQ] = rootP;
                size[rootP] += size[rootQ];
            } else {
                parent[rootP] = rootQ;
                size[rootQ] += size[rootP];
            }
            count--;
        }

        /* 判断 p 和 q 是否连通 */
        public boolean connected(int p, int q) {
            int rootP = find(p);
            int rootQ = find(q);
            return rootP == rootQ;
        }

        private int find(int x) {
            while (parent[x] != x) {
                // 进行路径压缩
                parent[x] = parent[parent[x]];
                x = parent[x];
            }
            return x;
        }

        /* 返回图中有多少个连通分量 */
        public int count() {
            return count;
        }
    }

    class Solution {
        public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
            UF uf = new UF(s.length());
            for (List<Integer> pair : pairs) {
                int from = pair.get(0), to = pair.get(1);
                uf.union(from, to);
            }

            Map<Integer, PriorityQueue<Character>> lsp = new HashMap<>();
            for (int i = 0; i < s.length(); i++) {
                int parent = uf.find(i);
                if (lsp.containsKey(parent)) {
                    lsp.get(parent).add(s.charAt(i));
                } else {
                    PriorityQueue<Character> pq = new PriorityQueue<>();
                    pq.add(s.charAt(i));
                    lsp.put(parent, pq);
                }
            }

            StringBuilder res = new StringBuilder();
            for (int i = 0; i < s.length(); i++) {
                int parent = uf.find(i);
                PriorityQueue<Character> pq = lsp.get(parent);
                res.append(pq.poll());
            }

            return res.toString();
        }
    }

//    输入：s = "dcab", pairs = [[0,3],[1,2],[0,2]]
//    输出："abcd"
//    解释：
//    交换 s[0] 和 s[3], s = "bcad"
//    交换 s[0] 和 s[2], s = "acbd"
//    交换 s[1] 和 s[2], s = "abcd"

    public void run() {
        Solution solution = new Solution();
        List<List<Integer>> pairs = InputUtil.toDoubleIntegerList("[[0,3],[1,2]]");
        System.out.println(solution.smallestStringWithSwaps("dcab", pairs));
    }

    public static void main(String[] args) throws Exception {
        LC1202 an = new LC1202();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
